package com.example.neutonote2.ui.review;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.neutonote2.R;
import com.example.neutonote2.data.model.NoteWithTags;
import com.example.neutonote2.ui.notes.NotesAdapter;
import com.example.neutonote2.viewmodel.NoteViewModel;

import java.util.ArrayList;
import java.util.List;

public class ReviewFragment extends Fragment {

    private NoteViewModel noteViewModel;
    private RecyclerView recyclerView;
    private TextView textNoReviews;
    private NotesAdapter adapter;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_review, container, false);

        // 初始化视图
        recyclerView = root.findViewById(R.id.recycler_view_review);
        textNoReviews = root.findViewById(R.id.text_no_reviews);

        // 设置RecyclerView
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new NotesAdapter(new ArrayList<>(), noteItemClickListener);
        recyclerView.setAdapter(adapter);

        return root;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 初始化ViewModel
        noteViewModel = new ViewModelProvider(requireActivity()).get(NoteViewModel.class);

        // 加载需要复习的笔记
        loadReviewNotes();
    }

    private void loadReviewNotes() {
        noteViewModel.getNotesToReview().observe(getViewLifecycleOwner(), notes -> {
            if (notes != null && !notes.isEmpty()) {
                textNoReviews.setVisibility(View.GONE);
                recyclerView.setVisibility(View.VISIBLE);

                // 将普通笔记列表转换为带标签的笔记列表
                List<NoteWithTags> notesWithTags = new ArrayList<>();
                for (com.example.neutonote2.data.model.Note note : notes) {
                    NoteWithTags noteWithTags = new NoteWithTags();
                    noteWithTags.note = note;
                    noteWithTags.tags = new ArrayList<>(); // 临时设置为空列表
                    notesWithTags.add(noteWithTags);
                }

                adapter.updateNotes(notesWithTags);
            } else {
                textNoReviews.setVisibility(View.VISIBLE);
                recyclerView.setVisibility(View.GONE);
            }
        });
    }

    // 笔记项点击监听器
    private final NotesAdapter.OnNoteItemClickListener noteItemClickListener = noteWithTags -> {
        // 导航到笔记编辑界面
        NavController navController = Navigation.findNavController(requireActivity(), R.id.nav_host_fragment_content_main);
        Bundle args = new Bundle();
        args.putInt("noteId", noteWithTags.note.getId());
        navController.navigate(R.id.nav_note_edit, args);
    };
} 